package portal.dao;

import dao.PublicDao;
import dao.Query;
import dao.QueryCallBack;
import dao.QueryCallBackList;
import db.DBDruidManager;
import portal.beans.Gpu;
import portal.beans.Node;
import portal.mybatis.dao.service.HostService;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description //TODO
 * @Author HMY
 * @Date 2018/9/28 16:48
 **/
@Component
public class ScheduleNodeDetailDao {
	@Autowired
	ScheduleGpuDetailDao scheduleGpuDetailDao;
	@Autowired
	HostService hostService;
	/**
     * By黄梦阳 //TODO 判断是否有同name的节点
     * @Param [nodeName]
     * @return boolean
     **/
	@Deprecated
    public boolean isExistName1(String nodeName){
        String sql = "select * from schedule_node_detail where node_name = ?";
        Node node = (Node) Query.executeQueryObject(sql, new Object[]{nodeName}, new QueryCallBack() {
            @Override
            public Object doExecute(ResultSet rs) {
                Node node1 = new Node();
                try {
                    while (rs.next()) {
                        node1.setCpuRatio(rs.getString("cpu_ratio"));
                        node1.setRemain_mem(rs.getString("remain_mem"));
                        node1.setNodeId(rs.getInt("node_id"));
                        node1.setNodeName(rs.getString("node_name"));
                        node1.setNodeIp(rs.getString("node_ip"));
                        node1.setGpuId(rs.getInt("gpu_id"));
                        node1.setGroupName(rs.getString("group_name"));
                        node1.setStatus(rs.getString("status"));
                    }
                }catch (SQLException e){
                    e.printStackTrace();
                }
                return node1;
            }
        });
//        System.out.println(node.getNodeName());
        return node.getNodeName()==null?false:true;
    }
    public boolean isExistName(String nodeName){
    	return hostService.isNodeExist(nodeName);
    	//return false;
    }
    /**
     * By黄梦阳 //TODO 判断是否有同id的节点
     * @Param
     * @return
     **/
    @Deprecated
    public boolean isExistIp1(String nodeIp){
        String sql = "select * from schedule_node_detail where node_ip = ?";
        Node node = (Node) Query.executeQueryObject(sql, new Object[]{nodeIp}, new QueryCallBack() {
            @Override
            public Object doExecute(ResultSet rs) {
                Node node1 = new Node();
                try {
                    while (rs.next()) {
                        node1.setRemain_mem(rs.getString("remain_mem"));
                        node1.setCpuRatio(rs.getString("cpu_ratio"));
                        node1.setNodeId(rs.getInt("node_id"));
                        node1.setNodeName(rs.getString("node_name"));
                        node1.setNodeIp(rs.getString("node_ip"));
                        node1.setGpuId(rs.getInt("gpu_id"));
                        node1.setGroupName(rs.getString("group_name"));
                        node1.setStatus(rs.getString("status"));
                    }
                }catch (SQLException e){
                    e.printStackTrace();
                }
                return node1;
            }
        });
//        System.out.println(node.getNodeIp());
        return node.getNodeIp()==null?false:true;
    }

    public boolean isExistIp(String nodeIp){
    	return hostService.isIpExist(nodeIp);
    }

	/**
	 * By黄梦阳 //TODO 新增计算节点数据库操作
	 * 
	 * @Param [node] 计算节点
	 * @return boolean
	 **/
    @Deprecated
	public boolean addNode1(Node node) {
		String sql = "insert into schedule_node_detail(node_ip" + ",node_name" + ",gpu_id" + ",group_name"
				+ ",create_time" + ",status) values(?,?,?,?,now(),?)";
		int lines = Query.executeDML(sql, new Object[] { node.getNodeIp(), node.getNodeName(), node.getGpuId(),
				node.getGroupName(), node.getStatus() });
		// System.out.println("lines ==============="+lines);
		return lines > 0 ? true : false;
	}

	public boolean addNode(Node node) {
		return hostService.insertNode(node)>0;
	}
	/**
     * By黄梦阳 //TODO 查询所有计算节点
     * @Param []
     * @return java.util.List<java.lang.Object>
     **/
    public List<Object> getAllNode() {
        List<Object> nodeList = new ArrayList<>();
        String sql = "select * from schedule_node_detail";
        nodeList = Query.executeQueryList(nodeList, sql, null, new QueryCallBackList() {
            @Override
            public List<Object> doExecuteList(ResultSet rs, List<Object> results) {
                try {
                    while (rs.next()) {
                        Node node = new Node();
                        node.setNodeId(rs.getInt("node_id"));
                        node.setNodeName(rs.getString("node_name"));
                        node.setNodeIp(rs.getString("node_ip"));
                        int gpuId = rs.getInt("gpu_id");
                        node.setGpuId(gpuId);
                        //ScheduleGpuDetailDao scheduleGpuDetailDao = new ScheduleGpuDetailDao();
                        Gpu gpu = scheduleGpuDetailDao.getGpuById(gpuId);
                        
                        node.setGroupName(rs.getString("group_name"));
                        node.setStatus(rs.getString("status"));
                        node.setCpuRatio(rs.getString("cpu_ratio"));
                        node.setRemain_mem(rs.getString("remain_mem"));
                        results.add(node);
                    }
                }catch (SQLException e){
                    e.printStackTrace();
                }
                return results;
            }
        });
        return nodeList;
    }

    
	/**
     * By黄梦阳 //TODO 分页查询计算节点
     * @deprecated 方法已废弃，改用HostService调用
     * @Param [onePageCount, currentPage]
     * limit 当前页是第几个记录，每页多少记录
     * @return java.util.List<java.lang.Object>
     **/
    @Deprecated
    public List<Object> getPagedNodeList(Integer onePageCount, Integer currentPage) {
        String sql = "select * from schedule_node_detail order by create_time desc limit ?,?";
        List<Object> pagedNodeList = new ArrayList<>();
        pagedNodeList = Query.executeQueryList(pagedNodeList, sql
                , new Object[]{(currentPage - 1) * onePageCount, onePageCount}
                , new QueryCallBackList() {
                    @Override
                    public List<Object> doExecuteList(ResultSet rs, List<Object> results) {
                        try {
                            while (rs.next()) {
                                Node node = new Node();
                                node.setRemain_mem(rs.getString("remain_mem"));
                                node.setCpuRatio(rs.getString("cpu_ratio"));
                                node.setNodeId(rs.getInt("node_id"));
                                node.setNodeName(rs.getString("node_name"));
                                node.setNodeIp(rs.getString("node_ip"));
                                int gpuId = rs.getInt("gpu_id");
                                node.setGpuId(gpuId);
                                
                                //ScheduleGpuDetailDao scheduleGpuDetailDao = new ScheduleGpuDetailDao();
                                Gpu gpu = scheduleGpuDetailDao.getGpuById(gpuId);
                               
                                node.setGroupName(rs.getString("group_name"));
                                node.setStatus(rs.getString("status"));
                                results.add(node);
                            }
                        }catch (SQLException e){
                            e.printStackTrace();
                        }finally{
                            DBDruidManager.closeConnection();
                        }
                        return results;
                    }
                });
        return  pagedNodeList;
    }
    /**
     * By黄梦阳 //TODO 计算总页数
     * @deprecated 同getPagedNodeList一起不推荐使用
     * @Param [onePageCount] 每页的节点数
     * @return int
     **/
    @Deprecated
    public int getNodePageNum(Integer onePageCount) {
        int jobNum = this.getNodeNum();
        return (jobNum % onePageCount) == 0 ? (jobNum / onePageCount) : (jobNum / onePageCount + 1);
    }
 
    /**
     * By黄梦阳 //TODO 根据节点id查询节点
     * @Param [nodeId]
     * @return portal.beans.Node
     **/
    public Node getNodeByNodeId(int nodeId) {
        String sql = "select * from schedule_node_detail where node_id = ?";
        Node node = (Node) Query.executeQueryObject(sql, new Object[]{nodeId}, new QueryCallBack() {
            @Override
            public Object doExecute(ResultSet rs) {
                Node node1 = new Node();
                try {
                    while (rs.next()) {
                        node1.setRemain_mem(rs.getString("remain_mem"));
                        node1.setCpuRatio(rs.getString("cpu_ratio"));
                        node1.setNodeId(rs.getInt("node_id"));
                        node1.setNodeName(rs.getString("node_name"));
                        node1.setNodeIp(rs.getString("node_ip"));
                        node1.setGroupName(rs.getString("group_name"));
                        node1.setStatus(rs.getString("status"));
                        int gpuId = rs.getInt("gpu_id");
                        node1.setGpuId(gpuId);
                        //ScheduleGpuDetailDao scheduleGpuDetailDao = new ScheduleGpuDetailDao();
                        Gpu gpu = scheduleGpuDetailDao.getGpuById(gpuId);
                     
                    }
                }catch (SQLException e){
                    e.printStackTrace();
                }finally{
                    DBDruidManager.closeConnection();
                }
                return node1;
            }
        });
        return node;
    }
   /* public Node getNodeByNodeId1(int nodeId) {
    	 return hostService.selectByName(id);
    }*/
   
    /**
     * By黄梦阳 //TODO 修改计算节点信息
     * @Param [node, newNode]
     * @return boolean
     **/
    @Deprecated
    public boolean updateNode1(Node node, Node newNode) {
        String sql = "update schedule_node_detail set " +
                " node_name = ?" +
                " ,gpu_id = ?" +
                " ,group_name = ?" +
                " ,status = ?" +
                " ,node_ip = ?" +
                " ,cpu_ratio = ?" +
                " ,remain_mem = ? " +
                " where node_id = ?";
        int lines = Query.executeDML(sql,new Object[]{newNode.getNodeName()
                                                    ,newNode.getGpuId()
                                                    ,newNode.getGroupName()
                                                    ,newNode.getStatus()
                                                    ,newNode.getNodeIp()
                                                    ,newNode.getCpuRatio()
                                                    ,newNode.getRemain_mem()
                                                    ,node.getNodeId()});
        return lines>0?true:false;
    }
    public boolean updateNode(Node node,Node newNode){
    	newNode.setNodeId(node.getNodeId());
    	return hostService.updateNodeById(newNode)>0;
    }
	/**
	 * By黄梦阳 //TODO 得到计算节点总数
	 * @deprecated 同getNodePageList一同废弃
	 * @Param []
	 * @return int
	 **/
    @Deprecated
	public synchronized int getNodeNum() {
		String sql = "select count(*) from schedule_node_detail";
		return PublicDao.oneNum(sql, null);
	}

	

	/**
	 * By黄梦阳 //TODO 根据nodeName 查询计算节点信息
	 * 
	 * @param nodeName
	 * @return
	 */
	@Deprecated
	public Node getNodeByNodeName1(String nodeName) {
		String sql = "select * from schedule_node_detail where node_name = ?";
		Node node = (Node) Query.executeQueryObject(sql, new Object[] { nodeName }, new QueryCallBack() {
			@Override
			public Object doExecute(ResultSet rs) {
				Node node1 = new Node();
				try {
					while (rs.next()) {
						node1.setNodeId(rs.getInt("node_id"));
						node1.setNodeName(rs.getString("node_name"));
						node1.setNodeIp(rs.getString("node_ip"));
						int gpuId = rs.getInt("gpu_id");
						node1.setGpuId(gpuId);

						node1.setGroupName(rs.getString("group_name"));
						node1.setStatus(rs.getString("status"));
						node1.setRemain_mem(rs.getString("remain_mem"));
						node1.setCpuRatio(rs.getString("cpu_ratio"));
						
						//ScheduleGpuDetailDao scheduleGpuDetailDao = new ScheduleGpuDetailDao();
						Gpu gpu = scheduleGpuDetailDao.getGpuById(gpuId);
						node1.setGpuId(gpu.getGpuId());
						node1.setGpu(gpu);

					}
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
					DBDruidManager.closeConnection();
				}
				return node1;
			}
		});
		return node;
	}
	public Node getNodeByNodeName(String nodeName) {
		return hostService.selectByName(nodeName);
	}
	/**
	 * By黄梦阳 //TODO 根据节点ID删除指定节点
	 * 
	 * @Param [nodeId]
	 * @return boolean
	 **/
	@Deprecated
	public boolean deleteNode1(int nodeId) {
		String sql = "delete from schedule_node_detail where node_id = ?";
		int lines = Query.executeDML(sql, new Object[] { nodeId });
		return lines > 0 ? true : false;
	}
	public boolean deleteNode(int nodeId) {
		Node node =new Node();
		node.setNodeId(nodeId);
		return hostService.deleteById(node)>0;
	}
}
